package com.netrust.quarz.task.utils;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.netrust.quarz.entity.Memory;
import com.netrust.quarz.entity.Server;
import com.netrust.quarz.entity.Storage;
import com.netrust.quarz.entity.VCpu;
import com.netrust.quarz.utils.ProdUtils;

public class StateConverUtils {
	private List<VCpu> cpuList=Lists.newArrayList();
	private List<Memory> memList=Lists.newArrayList();
	private List<Storage> stoList=Lists.newArrayList();
	
	public void convert(List<Server> servers,String json){
		@SuppressWarnings("unchecked")
		Map<String,Object> map=JSONObject.parseObject(json, Map.class);
		
		//空返回或延期判断
		if(map==null){
			return;
		}
		
		for (Server server : servers) {
			for (String key : map.keySet()) {
				JSONObject jo=JSONObject.parseObject(map.get(key).toString());
				
				VCpu vc=new VCpu();
				vc.setLastUpdateTime(new Date());
				vc.setAddTime(new Date());
				vc.setCpuUsage(ProdUtils.Usage.cpu(jo));
				
				
				Memory mem=new Memory();
				mem.setLastUpdateTime(new Date());
				mem.setAddTime(new Date());
				mem.setMemUsage(ProdUtils.Usage.mem(jo));
				
				Storage sto=new Storage();
				sto.setLastUpdateTime(new Date());
				sto.setAddTime(new Date());
				sto.setDiskUsage(ProdUtils.Usage.disk(jo));
				
				if(key.equals(server.getObjectId())){
					vc.setCarrier(server.getCarrier());
					vc.setDept(server.getDept());
					vc.setSid(server.getId());
					cpuList.add(vc);
					
					//内存对象
					mem.setCarrier(server.getCarrier());
					mem.setDept(server.getDept());
					mem.setSid(server.getId());
					mem.setMemOccupied(server.getMemoryTotal()*mem.getMemUsage()/100);
					mem.setFreeMem(server.getMemoryTotal()-mem.getMemOccupied());
					memList.add(mem);
					
					
					//存储对象
					sto.setCarrier(server.getCarrier());
					sto.setDept(server.getDept());
					sto.setSid(server.getId());
					sto.setDistTotal(server.getDiskTotal());
					sto.setDiskOccupied(server.getDiskTotal()*sto.getDiskUsage()/100);
					sto.setFreeDisk(sto.getDistTotal()-sto.getDiskOccupied());
					
					stoList.add(sto);
					
					break;
				}
			}
			
		}
		
	}

	public List<VCpu> getCpuList() {
		return cpuList;
	}

	public void setCpuList(List<VCpu> cpuList) {
		this.cpuList = cpuList;
	}

	public List<Memory> getMemList() {
		return memList;
	}

	public void setMemList(List<Memory> memList) {
		this.memList = memList;
	}

	public List<Storage> getStoList() {
		return stoList;
	}

	public void setStoList(List<Storage> stoList) {
		this.stoList = stoList;
	}
}
